<?php

namespace app\api\controller\offline_store;

use app\api\controller\AuthController;
use app\api\service\ShopsService;
use app\store\model\ApplyOfflineShop;
use think\exception\ValidateException;
use think\facade\Request;

class StoreController extends AuthController
{
    /**
     * 店铺修改记录
     */
    public function storesModifyRecord()
    {
        $param = $this->request->param();
        $user_id = $this->request->user['user_id'];
        $store_id = empty($param['store_id']) ? 1 : $param['store_id'];
        $shopInfo = ShopsService::getShopInfo($user_id, $store_id);
        if (empty($shopInfo)) {
            return json(['code' => 1, 'data' => [], 'msg' => '查询成功']);
        }
       
        $offline_shop_id = $shopInfo['id'];
        $applyOfflineShop = new ApplyOfflineShop();
       
        $list = $applyOfflineShop->field('id,desc,create_time,update_time')->where(['offline_shop_id' => $offline_shop_id, 'type' => 2])->order('create_time', 'desc')->paginate(10);
        return json(['code' => 1, 'data' => $list, 'msg' => '查询成功!']);
    }

    /**
     * 我的商圈商品列表
     */
    public function goodsList()
    {
        $param = $this->request->param();
        if (empty($param['store_id'])) {
            return json(['code' => 0, 'msg' => '参数有误']);
        }
        $user_id = $this->request->user['user_id'];
        $shopInfo = ShopsService::getShopInfo($user_id, $param['store_id']);
        if (empty($shopInfo)) {
            return json(['code' => 0, 'data' => '', 'msg' => '未找到店铺信息']);
        }
        if ($shopInfo['status'] != 2) {
            return json(['code' => 0, 'data' => '', 'msg' => '店铺未审核通']);
        }
        $offline_shop_id = $shopInfo['id'];
        $list = ShopsService::goodsList($offline_shop_id, $param['store_id']);
        return json(['code' => 1, 'data' => $list, 'msg' => '查询成功']);
    }

    /**
     * 删除商品
     */
    public function delGoods()
    {
        $param = $this->request->param();
        if (empty($param['gid'])) {
            return json(['code' => 0, 'msg' => '请选择要修改的商品']);
        }
        if (empty($param['store_id'])) {
            return json(['code' => 0, 'msg' => '参数有误']);
        }
        $user_id = $this->request->user['user_id'];
        $shopInfo = ShopsService::getShopInfo($user_id, $param['store_id']);
       
        if (empty($shopInfo)) {
            return json(['code' => 0, 'data' => '', 'msg' => '未找到店铺信息']);
        }

        if ($shopInfo['status'] != 2) {
            return json(['code' => 0, 'data' => '', 'msg' => '店铺未审核通过，不能删除商品']);
        }

        if ($shopInfo['s_status'] != 1) {
            return json(['code' => 0, 'data' => '', 'msg' => '店铺被禁用，请联系客服处理']);
        }

        $param['offline_shop_id'] = $shopInfo['id'];
        $goods_info = ShopsService::goodsInfo($param['gid'], $param['store_id']);
        if (empty($goods_info)) {
            return json(['code' => 0, 'data' => '', 'msg' => '未找到指定商品']);
        }
        $rst = ShopsService::delGoods($param['gid'], $param);
        return $rst ? json(['code' => 1, 'data' => '', 'msg' => '删除成功']) : json(['code' => 0, 'data' => '', 'msg' => '删除失败']);
    }

    /**
     * 修改商品
     */
    public function editGoods()
    {
        $param = $this->request->param();
        if (empty($param['gid'])) {
            return json(['code' => 0, 'msg' => '请选择要修改的商品']);
        }
        try {
            validate('ApplyShopValidate')->scene('add_goods')->check($param);
        } catch (ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }
        $user_id = $this->request->user['user_id'];
        $shopInfo = ShopsService::getShopInfo($user_id, $param['store_id']);
       
        if (empty($shopInfo)) {
            return json(['code' => 0, 'data' => '', 'msg' => '未找到店铺信息']);
        }

        if ($shopInfo['status'] != 2) {
            return json(['code' => 0, 'data' => '', 'msg' => '店铺未审核通过，不能修改商品']);
        }

        if ($shopInfo['s_status'] != 1) {
            return json(['code' => 0, 'data' => '', 'msg' => '店铺被禁用，请联系客服处理']);
        }

        $param['offline_shop_id'] = $shopInfo['id'];
        $goods_info = ShopsService::goodsInfo($param['gid'], $param['store_id']);
        if (empty($goods_info)) {
            return json(['code' => 0, 'data' => '', 'msg' => '未找到指定商品']);
        }
        $rst = ShopsService::editGoods($param['gid'], $param);
        return $rst ? json(['code' => 1, 'data' => '', 'msg' => '编辑成功']) : json(['code' => 0, 'data' => '', 'msg' => '编辑失败']);

    }

    /**
     * 添加商品
     */
    public function addGoods()
    {
        $param = $this->request->param();
        try {
            validate('ApplyShopValidate')->scene('add_goods')->check($param);
        } catch (ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }
        $user_id = $this->request->user['user_id'];
        $shopInfo = ShopsService::getShopInfo($user_id, $param['store_id']);
        if (empty($shopInfo)) {
            return json(['code' => 0, 'data' => '', 'msg' => '未找到店铺信息']);
        }
        if ($shopInfo['status'] != 2) {
            return json(['code' => 0, 'data' => '', 'msg' => '店铺未审核通过，不能上传商品']);
        }
        if ($shopInfo['s_status'] != 1) {
            return json(['code' => 0, 'data' => '', 'msg' => '店铺被禁用，请联系客服处理']);
        }

        $param['offline_shop_id'] = $shopInfo['id'];
        $rst = ShopsService::saveGoods($param);
        return $rst ? json(['code' => 1, 'data' => '', 'msg' => '添加成功']) : json(['code' => 0, 'data' => '', 'msg' => '添加失败']);
    }

    /**
     * 我的店铺信息
     */
    public function shopInfo()
    {
        $param = $this->request->param();
        $user_id = $this->request->user['user_id'];
        $store_id = empty($param['store_id']) ? 1 : $param['store_id'];
        $shopInfo = ShopsService::getShopInfo($user_id, $store_id);
        if ($shopInfo['s_status'] != 1) {
            return json(['code' => 0, 'data' => '', 'msg' => '店铺被禁用，请联系客服处理']);
        }
        return json(['code' => 1, 'data' => $shopInfo, 'msg' => '查询成功']);
    }

    /**
     * 修改店铺
     */
    public function editShop()
    {
        $param = $this->request->param();
        $fields = ['legal_person', 'category_id', 'contact_phone', 'location_address','shop_name', 'province', 'city', 'area', 'address',
            'business_license', 'logo', 'bg_imgs', 'lat', 'lng', 'store_id','label_id'];
        $param = Request::only($fields);
        $user_id = $this->request->user['user_id'];

        $store_id = $param['store_id'];
        try {
            validate('ApplyShopValidate')->scene('apply')->check($param);
        } catch (ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }

        $shopInfo = ShopsService::getShopInfo($user_id, $store_id);
        if (empty($shopInfo)) {
            return json(['code' => 0, 'data' => '', 'msg' => '店铺不存在!']);
        }
        if (!empty($shopInfo) && $shopInfo['status'] != 2) {
            return json(['code' => 0, 'data' => '', 'msg' => '店铺未审核成功，不能修改']);
        }
        if ($shopInfo['s_status'] != 1) {
            return json(['code' => 0, 'data' => '', 'msg' => '店铺被禁用，请联系客服处理']);
        }

        $applyOfflineShop = new ApplyOfflineShop();

        $is_exit_apply = $applyOfflineShop->where(['uid' => $user_id, 'offline_shop_id' => $shopInfo['id'], 'type' => 2, 'status' => 1])->order('create_time', 'desc')->value('id');
        if ($is_exit_apply) {
            return json(['code' => 0, 'data' => '', 'msg' => '存在待审核的申请，请等待审核!']);
        }

        $is_exit_month = $applyOfflineShop->where(['uid' => $user_id, 'offline_shop_id' => $shopInfo['id'], 'type' => 2, 'status' => 2])
            ->whereMonth('create_time')
            ->order('create_time', 'desc')->value('id');
        if ($is_exit_month) {
            return json(['code' => 0, 'data' => '', 'msg' => '每月只可修改一次!']);
        }

        $rst = $applyOfflineShop->editShop($fields, $user_id, $store_id, $param, $shopInfo['id']);
        if ($rst) {
            return json(['code' => 1, 'data' => '', 'msg' => '提交成功待审核!']);
        } else {
            return json(['code' => 0, 'data' => '', 'msg' => '提交失败!']);
        }
    }

    /**
     *
     * 申请店铺
     * */
    public function applyShop()
    {
        $param = $this->request->param();
        $param = Request::only(['legal_person', 'category_id', 'location_address','contact_phone', 'shop_name', 'province', 'city', 'area', 'address',
            'business_license', 'logo', 'bg_imgs', 'lat', 'lng', 'store_id', 'label_id']);
        $user_id = $this->request->user['user_id'];
        try {
            validate('ApplyShopValidate')->scene('apply')->check($param);
        } catch (ValidateException $e) {
            return json(['code' => 0, 'msg' => $e->getError()]);
        }

        $shopInfo = ShopsService::getShopInfo($user_id, $param['store_id']);
        if (!empty($shopInfo) && $shopInfo['status'] == 1) {
            return json(['code' => 0, 'data' => '', 'msg' => '店铺审核中，请勿重复提交!']);
        }
        if (!empty($shopInfo) && $shopInfo['status'] == 2) {
            return json(['code' => 0, 'data' => '', 'msg' => '店铺已存在，请勿重复申请!']);
        }
        $param['uid'] = $user_id;
        $rest = ShopsService::saveShop($param);
        if ($rest) {
            return json(['code' => 1, 'data' => '', 'msg' => '申请成功待审核!']);
        } else {
            return json(['code' => 0, 'data' => '', 'msg' => '申请失败!']);
        }
    }
}
